package it.francescogabbrielli.android.solidalapp.actions;

import android.util.Log;
import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.deviceendpoint.Deviceendpoint;
import it.francescogabbrielli.android.solidalapp.deviceendpoint.model.Device;

/**
 * Update device data
 */
public class EndpointsUpdateDevice extends EndpointsInsertDevice {
	
	private Device device;
	
	@Override
	public void execute() throws Exception {
		
		Device device = getModel(Device.class);
		if(device==null)
			return;
		
		Log.d(getClass().getSimpleName(), "Updating device");
		fillDevice(device);
		Deviceendpoint endpoint = EndpointsUtils.buildEndpoint(Deviceendpoint.class);
		this.device = endpoint.updateDevice(device).execute();
		setModel(this.device);
	}

	@Override
	public void onPostExecute() throws Exception {
		Log.d(getClass().getSimpleName(), "Updated device");
		if(device!=null)
			app.setData(DataKey.DEVICE, device);
	}

}
